#include "DictProducer.hpp"
#include "utils.h"
#include <iostream>
#include <sstream>
#include <fstream>

namespace wd
{

DictProducer::DictProducer(const char *dir)
: dir_(dir)
{
	get_files();
}

DictProducer::DictProducer(const std::string &dir)
: dir_(dir)
{
	get_files();
}

DictProducer::DictProducer(const std::string & dir, SplitTool * splitTool)
: dir_(dir)
, splitTool_(splitTool)
{
	get_files();
}

void DictProducer::build_dict()
{
	for(std::size_t idx = 0; idx != files_.size(); ++idx)
	{
		std::ifstream ifs(files_[idx].c_str());
		if(!ifs)
		{
			std::cout << "stream error, file does not exist or other " << std::endl;
			continue;
		}

		std::stringstream ss;
		std::string line;
		while(getline(ifs, line))
		{
			ss << line;
			std::string word;
			while(ss >> word)
			{
				//push_dict(word);
#if 1//english words
				word = ::upper_to_lower(word);
				if(word == std::string())
					continue;
				push_dict(word);
#endif 
			}
			ss.clear();
		}
	}
}

void DictProducer::build_cn_dict()
{
	for(int idx = 0; idx != files_.size(); ++idx)
	{
		std::ifstream ifs(files_[idx].c_str());
		if(!ifs)
		{
			std::cout << "stream error, file does not exist or other " << std::endl;
			continue;
		}

		//先获取一篇文章的总长度
		ifs.seekg(0, ifs.end);
		int length = ifs.tellg();
		ifs.seekg(0, ifs.beg);

		std::vector<std::string> pageWords;
		string content;
		content.reserve(length);
		ifs.read(&*content.begin(), length);
		pageWords = splitTool_->cut(content);
		for(int idx = 0; idx != pageWords.size(); ++idx)
		{
			push_dict(pageWords[idx]);
		}
		ifs.close();
	}
}

void DictProducer::show_files() const
{
	std::vector<std::string>::const_iterator vit;
	for(vit = files_.begin(); vit != files_.end(); ++vit)
	{
		std::cout << *vit << std::endl;
	}
}

void DictProducer::show_dict() const
{
	auto mit = dict_.begin();
	for(; mit != dict_.end(); ++mit)
	{
		std::cout << mit->first
				  << ' '
				  << mit->second
				  << '\n';
	}
}

void DictProducer::store_dict(const char *filepath)
{
	std::ofstream ofs(filepath);
	if(!ofs)
	{
		std::cout << "ofstream error" << std::endl;
		return ;
	}

	std::stringstream ss;
	auto mit = dict_.begin();
	for(; mit != dict_.end(); ++mit)
	{
		int ifreq = mit->second;
		std::string sec;
		ss << ifreq;
		ss >> sec;
		std::string line;
		line += mit->first;
		line += " ";
		line += sec;
		line += "\r\n";
		std::cout << line;
		ofs << line;
		ss.clear();
	}
}


//private
void DictProducer::get_files()
{
	std::cout << "dir_:" << dir_ << std::endl;
	::get_files_in_dir(dir_.c_str(), 0, files_);
}

#if 0
void DictProducer::produce_cn_dict(std::map<std::string, int> &page_words)
{
	auto iter = page_words.begin();
	for(; iter != page_words.end(); ++iter)
	{
		auto dit = dict_.find(iter->first);
		if(dit != dict_.end())
		{
			dit->second += iter->second;
		}
		else
		{
			dict_.insert(make_pair(iter->first, 1));
		}
	}
}
#endif

void DictProducer::push_dict(const std::string &word)
{
	auto iter = dict_.find(word);
	if(iter != dict_.end())
	{
		++(iter->second);
	}
	else
	{
		dict_.insert(make_pair(word, 1));
	}
}

}//end of namespace wd

